package org.example.controller;

import cn.hutool.core.lang.UUID;
import cn.lingyangwl.framework.core.response.Resp;
import org.example.annotation.ApiIdempotent;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author shenguangyang
 */
@RestController
public class ApiController {

    AtomicInteger num = new AtomicInteger(100);

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 前端获取requestId, 然后把该token放入请求的header中
     */
    @GetMapping("/getRequestId")
    public Resp<String> getRequestId() {
        String requestId = UUID.randomUUID().toString();
        stringRedisTemplate.opsForValue().set(requestId, "1");
        return Resp.ok(requestId);
    }

    /**
     * 主业务逻辑, num--, 并且加了自定义接口
     */
    @GetMapping("/submit")
    @ApiIdempotent
    public Resp<?> submit() {
        num.decrementAndGet();
        return Resp.ok();
    }

    @GetMapping("/getNum")
    public Resp<Integer> getNum() {
        return Resp.ok(num.get());
    }
}
